package com.tiankonguse.webbrowser;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.webkit.WebSettings;
import android.webkit.WebView;

@SuppressLint("NewApi")
public class MainUI extends Activity {

	private WebView webView = null;
	WebSettings webSettings = null;
	private Context context = null;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		this.webView = (WebView) this.findViewById(R.id.webview);
		this.webSettings = this.webView.getSettings();
		
		this.context= this;
		

		/*
		 * 函数：setSupportZoom
		 * 作用：允许使用网页缩放功能
		 * 默认值：true
		 * 推荐：设为true
		 */
		this.webSettings.setSupportZoom(true);
		

		/*
		 * 函数：setBuiltInZoomControls
		 * 作用：启动网页缩放功能，但是开启后有个放大和缩小的控件
		 * 默认值：false
		 * 推荐：设为true
		 */
		this.webSettings.setBuiltInZoomControls(true);
		
 
		/*
		 * 函数：setDisplayZoomControls
		 * 作用：如果开启了网页缩放功能后,控制是否显示缩放控件
		 * 默认值：true
		 * 推荐：设为false
		 */
		this.webSettings.setDisplayZoomControls(false);

		/*
		 * 函数：setJavaScriptEnabled
		 * 作用：启用javascript
		 * 默认值：false
		 * 推荐：设为true
		 */
		this.webSettings.setJavaScriptEnabled(true);
		
		 
		this.webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
		
		this.webView.addJavascriptInterface(new WebAppInterface(this), "Android");
		
		this.webView.loadUrl("http://tiankonguse.com/");
		
		/*
		 * 自己控制用户点击链接的响应
		 */
		this.webView.setWebViewClient(new MyWebViewClient(this.context));
		 
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
	    // Check if the key event was the Back button and if there's history
	    if ((keyCode == KeyEvent.KEYCODE_BACK) && this.webView.canGoBack()) {
	    	this.webView.goBack();
	        return true;
	    }
	    // If it wasn't the Back key or there's no web page history, bubble up to the default
	    // system behavior (probably exit the activity)
	    return super.onKeyDown(keyCode, event);
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}
}
